package com.egame.manage.biz.server.gc;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.common.model.PageData;
import cn.egame.interfaces.gc.TerminalRecognitionInfo;
import com.egame.manage.biz.server.ManageClient;

public class TerminalRecognitionServiceImpl {
	private static TerminalRecognitionServiceImpl instance = null;
	private static byte[] SyncRoot = new byte[1];
	private TerminalRecoginitionServiceDao dao = null;
	
	public static TerminalRecognitionServiceImpl getInstance() throws ExceptionCommonBase {
		if (instance == null) {
			synchronized (SyncRoot) {
				if (instance == null) {
					instance = new TerminalRecognitionServiceImpl();
				}
			}
		}
		return instance;
	}
	
	public TerminalRecognitionServiceImpl() throws ExceptionCommonBase{
		super();
		dao = new TerminalRecoginitionServiceDao();
	}

	public int setTerminalRecognitionInfo(int appId, long loginUserId, TerminalRecognitionInfo bean) throws RemoteException {
		if(bean != null){
			TerminalRecognitionInfo beanInfo = ManageClient.getInstance().getTerminalRecognitionInfo(appId, loginUserId, bean.getApiCode(), bean.getUaCode(), bean.getScreenPx());
			if(beanInfo == null){
				return dao.setTerminalRecognitionInfo(bean);
			}
		}
		return -1;
	}	

	public int updateTerminalRecognitionInfo(int appId, long loginUserId, TerminalRecognitionInfo bean) throws ExceptionCommonBase {
		return dao.updateTerminalRecognitionInfo(bean);
	}
	
	public int deleteTerminalRecognitionInfo(String apiCode, String uaCode, String screenPx) throws ExceptionCommonBase{
		return dao.deleteTerminalRecognitionInfo(apiCode, uaCode, screenPx);
	}
	
	public int deleteTerminalRecognitionInfo(int terminalId) throws ExceptionCommonBase {
		return dao.deleteTerminalRecognitionInfo(terminalId);
	}
	
	public int getTypecalTerminalId()throws ExceptionCommonBase{
		return dao.getTypecalTerminalId();
	}
	
	public PageData pageTerminalRecognition(int appId, long loginUserId,int page,int rowsOfPage, TerminalRecognitionInfo bean,int manufatureId)throws RemoteException{
		List<String[]> ids = dao.listTerminalRecognition(bean, manufatureId, page, rowsOfPage);
		List<TerminalRecognitionInfo> list = new ArrayList<TerminalRecognitionInfo>();
		if(ids != null && ids.size()>0){
			TerminalRecognitionInfo info = null;
			for(String[] id:ids){
				if(id.length == 3){
					info = ManageClient.getInstance().getTerminalRecognitionInfo(appId, loginUserId, id[0], id[1], id[2]);
					list.add(info);
				}
			}
		}
		int total = dao.getTerminalRecognitionCount(bean, manufatureId);
		PageData pd = new PageData(page, total, rowsOfPage);
		pd.setContent(list);
		return pd;
	}
}
